package com.sogoodsoft.web.crm;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.utils.encode.JsonBinder;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.sogoodsoft.entity.account.User;
import com.sogoodsoft.entity.crm.Alert;
import com.sogoodsoft.service.account.AccountManager;
import com.sogoodsoft.service.common.AbstractService;
import com.sogoodsoft.service.crm.AlertService;
import com.sogoodsoft.util.DwzMsg;
import com.sogoodsoft.util.InfosysSecurityUtil;
import com.sogoodsoft.web.BaseAction;

@Results({ @Result(name = "AlertContent", location = "alertContent.jsp") })
public class AlertAction extends BaseAction < Alert, Long > {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Autowired
    private AlertService alertService;

    @Autowired
    private AccountManager accountManager;

    private boolean editable = true;

    private static JsonBinder binder = JsonBinder.buildNonDefaultBinder();

    @SuppressWarnings("rawtypes")
    @Override
    protected AbstractService getService() {
        return alertService;
    }

    @Override
    public String input() throws Exception {
        if (entity.getId() != null) {
            editable = alertService.editable(entity);
            if(!InfosysSecurityUtil.isAdmin()){
            	User user = InfosysSecurityUtil.getCurrentUser();	
            	if(user.getId().compareTo(entity.getUserid())!=0){
            		editable = false;
            	}
            }
        }
        
        return INPUT;
    }
    

    @SuppressWarnings("unchecked")
	@Override
	public String delete() throws Exception {
    	 if(!InfosysSecurityUtil.isAdmin()){
    		 entity = alertService.get(id);
         	User user = InfosysSecurityUtil.getCurrentUser();	
         	if(user.getId().compareTo(entity.getUserid())!=0){
         		Struts2Utils.renderJson(DwzMsg.fail("对不起，不能删除他人创建的提醒！"));
         		return null;
         	}
         }
    	  getService().delete(id);
          Struts2Utils.renderJson(DwzMsg.success());
          return null;
	}

	public String sysAlert() {
        List < Alert > list = alertService.getCurrUserSysAlert();
        String json = binder.toJson(list);
        logger.debug("日程提醒的定时提醒JSON");
        logger.debug(json);
        Struts2Utils.renderJson(json);
        return null;
    }

    public String loginAlert() throws Exception {
        List < Alert > list = alertService.getCurrUserLoginAlert();
        String json = binder.toJson(list);
        logger.debug("日程提醒的登录提醒JSON");
        logger.debug(json);
        Struts2Utils.renderJson(json);
        return null;
    }

    public String AlertContent() throws Exception {
        prepareModel();
        alertService.AlertPublished(entity.getId());
        return "AlertContent";
    }

    public boolean isEditable() {
        return editable;
    }

    public void setEditable(boolean editable) {
        this.editable = editable;
    }

	@SuppressWarnings("unchecked")
	@Override
	public String list() throws Exception {
		if(InfosysSecurityUtil.isAdmin()){
		page = alertService.keyQuery("list.alert",page, Struts2Utils.getRequest().getParameterMap());
		}else{
			Map<String, String> map = new HashMap<String, String>();
			map.putAll(Struts2Utils.getRequest().getParameterMap());
			User user = InfosysSecurityUtil.getCurrentUser();			
			map.put("userid", String.valueOf(user.getId()));
			page = alertService.keyQuery("list.alert",page, map);
		}
		 return SUCCESS;
	}

    
}
